// a library to wrap and simplify api calls
import { DeviceEventEmitter } from 'react-native'
import apisauce from 'apisauce'
import WINDOW from './Windows'
import underscore from 'underscore'
var qs = require('querystringify')
import * as RootNavigation from '../Navigation/RootNavigation.js'

// our "constructor"
const create = (baseURL = WINDOW.baseURL) => {
  // ------
  // STEP 1
  // ------
  //
  // Create and configure an apisauce-based api object.
  //
  const api = apisauce.create({
    // base URL is read from the "constructor"
    baseURL,
    // here are some default headers
    headers: {
      'Cache-Control': 'no-cache',
    },
    // 10 second timeout...
    timeout: 100000
  })

  const checkTokenMonitor = (response) => {
    console.log(response)
  }
  api.addMonitor(checkTokenMonitor)

  // ------
  // STEP 2
  // ------
  //
  // Define some functions that call the api.  The goal is to provide
  // a thin wrapper of the api layer providing nicer feeling functions
  // rather than "get", "post" and friends.
  //
  // I generally don't like wrapping the output at this level because
  // sometimes specific actions need to be take on `403` or `401`, etc.
  //
  // Since we can't hide from that, we embrace it by getting out of the
  // way at this level.
  //  
  // const login = (phone, code) => api.post('open/index/login', { phone, code })
  const goodsCataList = (appID = '642', number = '100', direct = '-1', lastID = '0') => api.get('goods/goodsCataList.jsp', { appID, number, direct, lastID })
  const goodsByCataCode = (cataCode, number = '100', appID = '642', direct = '-1', lastID = '0') => api.get('goods/goodsByCataCode30.jsp', { cataCode, appID, number, direct, lastID })
  const goodsDetails = (id) => api.get('goods/goodsDetails30.jsp', { id })
  const searchGoods = (keyWord, appID = '642', number = '100', direct = '-1', lastID = '0') => api.get('goods/searchGoods.jsp', { keyWord, appID, number, direct, lastID })
  const indexName = (appID = '642') => api.get('indexName.jsp', { appID})  

  const articleCata = (appID = '642') => api.get('article/articleCata.jsp', { appID })
  const articleList = (typeID, number = '100', appID = '642', direct = '-1', lastID = '0') => api.get('article/articleList.jsp', { typeID, appID, number, direct, lastID })
  const articleDetail = (ID, appID = '642') => api.get('article/articleDetail30.jsp', { ID, appID })


  const CreateBuySellCollect = (appID = '642', number = '100', direct = '-1', lastID = '0') => api.get('goods/CreateBuySellCollect.jsp', { appID, number, direct, lastID })
  // const goodsCataList = (appID = '642', number = '100', direct = '-1', lastID = '0') => api.post('goods/goodsCataList30.jsp', { appID, number, direct, lastID })
  // const goodsCataList = (appID = '642', number = '100', direct = '-1', lastID = '0') => api.post('goods/goodsCataList30.jsp', { appID, number, direct, lastID })

  // ------
  // STEP 3
  // ------
  //
  // Return back a collection of functions that we would consider our
  // interface.  Most of the time it'll be just the list of all the
  // methods in step 2.
  //
  // Notice we're not returning back the `api` created in step 1?  That's
  // because it is scoped privately.  This is one way to create truly
  // private scoped goodies in JavaScript.
  //
  return {
    // a list of the API functions from step 2
    api,
    goodsCataList,  
    goodsByCataCode,
    goodsDetails,
    searchGoods,
    indexName,
    CreateBuySellCollect,

    articleCata,
    articleList,
    articleDetail
  }

}

// let's return back our create method as the default.
export default {
  create
}


